<template>
	<jk-dialog
		v-model="showDialog"
		:title="`${dataId ? '编辑' : '新增'}-设备机型`"
		width="500px"
		:show-content-loading="showLoading"
		:confirm-btn-loading="saveBtnLoading"
		append-to-body
		@on-cancel="onVisibleChange(false)"
		@on-visible-change="onVisibleChange"
		@on-confirm="onConfirmBtn"
	>
		<el-form ref="ruleForm" :model="ruleForm" :rules="rules" label-width="110px" :show-message="false">
			<el-row>
				<el-col :span="22">
					<el-form-item class="margin-bottom-10" label="编号" prop="code">
						<el-input v-model="ruleForm.code" placeholder="编号" />
					</el-form-item>
				</el-col>
				<el-col :span="22">
					<el-form-item class="margin-bottom-10" label="名称" prop="name">
						<el-input v-model="ruleForm.name" placeholder="名称" />
					</el-form-item>
				</el-col>
				<!--<el-col :span="22">
					<el-form-item class="margin-bottom-10" label="功率" prop="power">
						<jk-number-input v-model="ruleForm.power" placeholder="功率" />
					</el-form-item>
				</el-col>
				<el-col :span="22">
					<el-form-item class="margin-bottom-10" label="锭数" prop="spindleNum">
						<jk-number-input v-model="ruleForm.spindleNum" placeholder="锭数" />
					</el-form-item>
				</el-col>
				<el-col :span="22">
					<el-form-item class="margin-bottom-10" label="变频器温度" prop="temp">
						<jk-number-input v-model="ruleForm.temp" placeholder="变频器温度" />
					</el-form-item>
				</el-col>
				-->
				<el-col :span="22">
					<el-form-item class="margin-bottom-10" label="工序">
						<el-select v-model="ruleForm.processId" class="width-100-percent" placeholder="工序">
							<el-option
								v-for="item in processList"
								:key="item.id"
								:label="item.name"
								:value="item.id"
							></el-option>
						</el-select>
					</el-form-item>
				</el-col>
				<el-col :span="22">
					<el-form-item class="margin-bottom-10" label="设备类别" prop="typeId">
						<el-select v-model="ruleForm.typeId" class="width-100-percent" placeholder="设备类别">
							<el-option
								v-for="item in typeList"
								:key="item.id"
								:label="item.name"
								:value="item.id"
							></el-option>
						</el-select>
					</el-form-item>
				</el-col>
			</el-row>
		</el-form>
	</jk-dialog>
</template>

<script>
    import calHeight from '/src/mixins/cal-table-height';
    import {
        spinMachineModelCreate,
        spinMachineModelUpdate,
        spinMachineModelGet
    } from '../../../../api/basicData/machineModel';
    import { systemBusinessDictPage } from '../../../../api/basicData/businessDict';
    import { spinProcessPage } from '../../../../api/technology/process';
    export default {
        name: 'MachineModel',
        props: {
            dialogState: {
                type: Boolean,
                default: false
            },
            dataId: {
                type: [String, Number],
                default: ''
            }
        },
        mixins: [calHeight],
        data() {
            return {
                processList: [],
                typeList: [],
                showDialog: false,
                showLoading: true,
                saveBtnLoading: false,
                ruleForm: {
                    id: null,
                    code: '',
                    name: '',
                    temp: null,
                    power: null,
                    processId: null,
                    processCode: '',
                    processName: '',
                    spindleNum: null,
                    typeId: null
                },
                rules: {
                    processId: [{ required: true, trigger: 'change' }],
                    power: [{ required: true, trigger: 'change' }],
                    code: [{ required: true, trigger: 'change' }],
                    temp: [{ required: true, trigger: 'change' }],
                    name: [{ required: true, trigger: 'change' }],
                    spindleNum: [{ required: true, trigger: 'change' }],
                    typeId: [{ required: true, trigger: 'change' }]
                }
            };
        },
        methods: {
            getParamData() {
                const params = JSON.parse(JSON.stringify(this.ruleForm));
                if (!this.dataId) delete params.id;
                params.typeName = this.typeList.find(x => x.id === params.typeId).name;
                const processItem = this.processList.find(x => x.id === params.processId) || {};
                params.processCode = processItem.code;
                params.processName = processItem.name;
                return params;
            },
            /** 提交按钮 */
            onConfirmBtn() {
                this.$refs['ruleForm'].validate(valid => {
                    if (valid) {
                        const api = !this.dataId ? spinMachineModelCreate : spinMachineModelUpdate;
                        this.saveBtnLoading = true;
                        api(this.getParamData()).then(res => {
                            this.saveBtnLoading = false;
                            if (res.data.status === 200) {
                                this.$tipsBar(this, 'save');
                                this.$emit('on-finish');
                            }
                        });
                    } else {
                        this.$tipsBar(this, 'validator');
                    }
                });
            },
            onVisibleChange(e) {
                this.$emit('on-visible-change', e);
                if (!e) {
                    Object.assign(this.$data, this.$options.data());
                    this.$nextTick(() => {
                        this.$refs.ruleForm.resetFields();
                    });
                }
            },
            getDetailRequest() {
                return spinMachineModelGet({ id: this.dataId }).then(res => {
                    if (res.data.status === 200) {
                        this.ruleForm = res.data.res;
                    }
                });
            },
            getTypeListRequest() {
                return systemBusinessDictPage({ categoryCode: 'MACHINE_TYPE' }).then(res => {
                    if (res.data.status === 200) {
                        this.typeList = res.data.res;
                    }
                });
            },
            getProcessListRequest() {
                return spinProcessPage({ auditState: 1 }).then(res => {
                    if (res.data.status === 200) {
                        this.processList = res.data.res;
                    }
                });
            },
            async getDependentData() {
                this.showLoading = true;
                await this.getProcessListRequest();
                await this.getTypeListRequest();
                if (this.dataId) await this.getDetailRequest();
                this.showLoading = false;
            }
        },
        watch: {
            dialogState(newVal) {
                this.showDialog = newVal;
                if (newVal) {
                    this.getDependentData();
                }
            }
        }
    };
</script>
